package com.icebartech.base.modules.common.api;

import com.icebartech.base.common.enums.BaseEnums;
import com.icebartech.base.modules.common.service.AttachmentInfoService;
import com.icebartech.core.annotations.RequireLogin;
import com.icebartech.core.components.AliyunOSSComponent;
import com.icebartech.core.constants.UserEnum;
import com.icebartech.core.controller.BaseController;
import com.icebartech.core.utils.CommonUtil;
import com.icebartech.core.vo.BaseData;
import com.icebartech.core.vo.FileInfo;
import com.icebartech.core.vo.RespJson;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@Api(description = "微信系统模块接口")
@RestController
@Slf4j
@RequestMapping(value = "/base/sys/mini", produces = "application/json;charset=UTF-8")
public class WeixinSysController extends BaseController {
    
    @Autowired
    private AliyunOSSComponent aliyunOSSComponent;
    
    @Autowired
    private AttachmentInfoService attachmentInfoService;
    
    /**
     * 上传文件
     * @param file
     * @return
     */
    private FileInfo updateFile(MultipartFile file, String targetType) {
        if (file != null && file.getSize() > 0) {
            String suffix = CommonUtil.getFileSuffix(file.getOriginalFilename());
            
            //上传文件到阿里云
            try {
                String fileKey = aliyunOSSComponent.uploadFile(file.getBytes(), suffix, file.getOriginalFilename());
                if (StringUtils.isNotBlank(fileKey)) {
                    String fileUrl = aliyunOSSComponent.getFileUrl(fileKey);
                    //保存到附件表
                    FileInfo fileInfo = new FileInfo();
                    fileInfo.setFileKey(fileKey);
                    fileInfo.setFileName(file.getOriginalFilename());
                    fileInfo.setFileUrl(fileUrl);
                    fileInfo.setTargetType(targetType);
                    FileInfo newInfo = attachmentInfoService.saveAttachmentFile(fileInfo);
                    return newInfo;
                }
            } catch (Exception e) {
                log.error(e.getMessage(), e);
            }
        }
        return null;
    }
    
    @ApiOperation("微信端上传文件")
    @RequireLogin(userEnum = UserEnum.weixin)
    @PostMapping(value = "/miniUploadFile")
    public RespJson<BaseData<FileInfo>> miniUploadFile(@RequestParam(value = "file") MultipartFile imgFile) {
        RespJson respJson = getFailRtnMap("发生未知错误");
        if (imgFile != null && imgFile.getSize() > 0) {
            FileInfo fileInfo = updateFile(imgFile, BaseEnums.AttachmentTargetType.mini.getKey());
            BaseData baseData = new BaseData(fileInfo);
            respJson = getSuccessRtnMap("上传文件成功！", baseData);
        } else {
            respJson = getFailRtnMap("上传文件不能为空");
        }
        return respJson;
    }
}
